package com.neulogistics.core.customercenter.api.application.integrationevent.handler;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.blackto.lib.eventbus.abstraction.IIntegrationEventHandler;
import com.neulogistics.core.customercenter.api.application.integrationevent.subscribe.TransferOrderCreatedIntegrationEvent;
import com.neulogistics.core.customercenter.domain.model.enumeration.OrderStatus;
import com.neulogistics.core.customercenter.infrastructure.persistence.orm.po.OrderPO;
import com.neulogistics.core.customercenter.infrastructure.persistence.orm.service.IOrderPOService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

@Component
@Slf4j
public class TransferOrderCreatedIntegrationEventHandler implements IIntegrationEventHandler<TransferOrderCreatedIntegrationEvent> {
    @Resource
    IOrderPOService orderPOService;

    @Override
    public CompletableFuture<Void> handle(TransferOrderCreatedIntegrationEvent event) {
        LambdaUpdateWrapper<OrderPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(OrderPO::getOrderId, event.orderId)
                .lt(OrderPO::getOrderStatus, OrderStatus.To_Be_Scheduled.id)
                .set(OrderPO::getOrderStatus, OrderStatus.To_Be_Scheduled.id);
        orderPOService.update(updateWrapper);

        return CompletableFuture.completedFuture(null);
    }
}
